package net.alegen.helloworld;

import android.os.Handler;
import android.widget.TextView;

public class TextThread extends Thread {
	
	private volatile boolean stop;
	private Handler mainThreadHandler;
	private TextView txtView;
	private String text = "This is a secondary thread! ";
	
	public TextThread(Handler mainThreadHandler, TextView txtView) {
		this.stop = false;
		this.mainThreadHandler = mainThreadHandler;
		this.txtView = txtView;
	}
	
	@Override
	public void run() {
		while(!this.stop) {
			mainThreadHandler.post(new Runnable() {						
				public void run() {
					char firstChar = text.charAt(0);
					StringBuilder sBuilder = new StringBuilder(text);
					sBuilder.deleteCharAt(0);
					sBuilder.append(firstChar);
					text = sBuilder.toString();
					txtView.setText(text);
				}
			});
			try {
				Thread.sleep(200);
			} catch (InterruptedException e) {
				break;
			}
		}
	}
	
	public void stopThread() {
		this.stop = true;
	}
}
